package com.portal.innovation.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.portal.innovation.common.Constant;
import com.portal.innovation.utils.PageUtils;
import com.portal.innovation.config.ConstPropertiesConfig;
import com.portal.innovation.dao.FileListDao;
import com.portal.innovation.pojo.FileList;
import com.portal.innovation.service.OSService;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.Map;

@Service
public class OSServiceImpl extends ServiceImpl<FileListDao, FileList> implements OSService {


    private String getFileName(String filName) {

        String[] s = filName.split(".");
        System.out.println(Arrays.toString(s));
        return s[s.length - 1];
    }

    @Override
    public void deleteFile(String fileUrl) {
        OSS client = new OSSClientBuilder().build(ConstPropertiesConfig.END_POINT,
                ConstPropertiesConfig.ACCESS_KEY_ID,
                ConstPropertiesConfig.ACCESS_KEY_SECRET);
        client.deleteObject(ConstPropertiesConfig.BUCKET_NAME, fileUrl.substring(fileUrl.lastIndexOf('/') + 1));
        client.shutdown();
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Integer curPage = 1;
        Integer limit = 10;

        if (params.get(Constant.PAGE) != null) {
            curPage = Integer.parseInt(params.get(Constant.PAGE).toString());
        }
        if (params.get(Constant.LIMIT) != null) {
            limit = Integer.parseInt(params.get(Constant.LIMIT).toString());
        }
        //分页对象
        Page<FileList> page = new Page<>(curPage, limit);

        QueryWrapper<FileList> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_date");
        PageUtils pageUtils = new PageUtils(this.page(page, wrapper));
        return pageUtils;
    }

}
